package com.cqyc.imview.activity

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import com.cqyc.imview.R
import com.cqyc.imview.StatusBarUtils
import com.cqyc.imview.base.BaseActivity
import com.cqyc.imview.databinding.ActivityMWebBinding
import com.cqyc.imview.fragment.MWebFragment
import com.cqyc.imview.model.MWebAModel

class MWebActivity : BaseActivity<ActivityMWebBinding, MWebAModel>(), View.OnClickListener {

    companion object {
        val URL = "urlPath"
        val TITLE = "title"
        val TITLEVISIBLE = "titleVisible"
    }

    private lateinit var fragment: MWebFragment

    override fun setLayout(): Int {
        return R.layout.activity_m_web
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        StatusBarUtils.setStatusBar(window)
        val title = intent.getStringExtra(TITLE)
        if (title != null)
            mBinding.iTitle.text = title
        val isVisible = intent.getBooleanExtra(TITLEVISIBLE, false)
        mBinding.titleGroup.isVisible = isVisible
        mBinding.iBack.setOnClickListener(this)

        fragment = MWebFragment()
        val begin = supportFragmentManager.beginTransaction()
        val url = intent.getStringExtra(URL)
        val bundle = Bundle()
        bundle.putString("urlPath", url)
        fragment.arguments = bundle
        begin.replace(R.id.fragment, fragment)
        begin.commitAllowingStateLoss()
    }

    override fun onClick(p0: View?) {
        when (p0!!.id) {
            R.id.i_back -> {
                finish()
            }
        }
    }
}